oo::class create PopupDialog {
    variable Path PopUpFlag OKVal CancelVal ifDestroy

    constructor {path {returnVals {1 0}}} {
        set Path $path
        set PopUpFlag {}
        set ifDestroy 0
        foreach {OKVal CancelVal} $returnVals {}
    }

    method turnOnFlag {} {
        set PopUpFlag $OKVal
    }

    method turnOffFlag {} {
        set PopUpFlag $CancelVal
    }

    method setDestroy {} {
        set ifDestroy 1
    }

    method createPopup {msg} {
        toplevel $Path

        ttk::label $Path.msg -text $msg -anchor c
        ttk::frame $Path.btns
        ttk::button $Path.btns.ok -text OK -underline 0 -command \
            "[self object] turnOnFlag; [self object] setDestroy"

        ttk::button $Path.btns.no -text NO -underline 0 -command \
            "[self object] turnOffFlag; [self object] setDestroy"

        bind $Path.btns.ok <Return> "[self object] turnOnFlag; [self object] setDestroy"
        bind $Path.btns.no <Return> "[self object] turnOffFlag; [self object] setDestroy"
        bind $Path <Escape> "[self object] setDestroy"
        wm protocol $Path WM_DELETE_WINDOW "[self object] setDestroy"

        pack $Path.msg -fill both -side top
        pack $Path.btns -fill both -side bottom
        pack $Path.btns.ok $Path.btns.no \
            -side left -expand 1

        wm resizable $Path 0 0
    }

    method popup {msg} {
        my createPopup $msg

        wm attribute $Path -toolwindow 1
        wm withdraw $Path
        my popupInCenter $Path [winfo parent $Path]

        if ![info exists PopUpFlag] { return {} }
        return $PopUpFlag
    }

    method fadeIn {tp percent} {
        if [winfo exists $tp] {
            wm attributes $tp -alpha $percent
            if {$percent < 1.0} {
                after 50 [list [self object] fadeIn $tp [expr $percent + 0.2]]
            }
        }
    }

    method getXY {tp parent} {
        set rootx [winfo rootx $parent]
        set rooty [winfo rooty $parent]
        set paWidth [winfo width $parent]
        set paHeight [winfo height $parent]
        set tpWidth [winfo reqwidth $tp]
        set tpHeight [winfo reqheight $tp]

        return [list \
          [expr $rootx+($paWidth-$tpWidth)/2] \
            [expr $rooty+($paHeight-$tpHeight)/2]]
    }
    
    method popupInCenter {tp parent} {
        # TOPMOST
        set ifTopmost [wm attributes $parent -topmost]
        if $ifTopmost {
            wm attributes $parent -topmost 0
        }

        foreach {x y} [my getXY $tp $parent] {}
        wm geometry $tp +$x+$y
        # my fadeIn $tp 0.0
        wm deiconify $tp
        grab $tp

        focus $tp.btns.ok
        # tkwait visibility $tp
        vwait [info object namespace [self object]]::ifDestroy
        destroy $tp

        # TOPMOST
        wm attributes $parent -topmost $ifTopmost
    }
}